/*
========================================================================
 Name        : SysSettingList.cpp
 Author      : shitou
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <avkon.hrh>
#include <avkon.rsg>
#include <eikmenup.h>
#include <aknappui.h>
#include <eikcmobs.h>
#include <barsread.h>
#include <stringloader.h>
#include <gdi.h>
#include <eikedwin.h>
#include <eikenv.h>
#include <aknnumedwin.h>
#include <aknpopupfieldtext.h>
#include <eikappui.h>
#include <aknviewappui.h>
#include <TaobaoMobile.rsg>
// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]
#include "SysSettingList.h"
#include "SysSettingListSettings.h"
#include "TaobaoMobile.hrh"
#include "LoginContainer.hrh"
#include "OrderListBox.hrh"
#include "QueryList.hrh"
#include "DetailContainer.hrh"
#include "SysSettingList.hrh"
#include "SysSettingListView.h"
// ]]] end generated region [Generated User Includes]

// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * Construct the CSysSettingList instance
 * @param aCommandObserver command observer
 */ 
CSysSettingList::CSysSettingList( 
		TSysSettingListSettings& aSettings, 
		MEikCommandObserver* aCommandObserver )
	: iSettings( aSettings ), iCommandObserver( aCommandObserver )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}
/** 
 * Destroy any instance variables
 */
CSysSettingList::~CSysSettingList()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}

/**
 * Handle system notification that the container's size has changed.
 */
void CSysSettingList::SizeChanged()
	{
	if ( ListBox() ) 
		{
		ListBox()->SetRect( Rect() );
		}
	}

/**
 * Create one setting item at a time, identified by id.
 * CAknSettingItemList calls this method and takes ownership
 * of the returned value.  The CAknSettingItem object owns
 * a reference to the underlying data, which EditItemL() uses
 * to edit and store the value.
 */
CAknSettingItem* CSysSettingList::CreateSettingItemL( TInt aId )
	{
	switch ( aId )
		{
	// [[[ begin generated region: do not modify [Initializers]
		case ESysSettingListViewServerAddress:
			{			
			CAknTextSettingItem* item = new ( ELeave ) 
				CAknTextSettingItem( 
					aId,
					iSettings.ServerAddress() );
			return item;
			}
		case ESysSettingListViewPort:
			{			
			CAknIntegerEdwinSettingItem* item = new ( ELeave ) 
				CAknIntegerEdwinSettingItem( 
					aId,
					iSettings.Port() );
			return item;
			}
		case ESysSettingListViewRemberPassword:
			{			
			CAknBinaryPopupSettingItem* item = new ( ELeave ) 
				CAknBinaryPopupSettingItem( 
					aId,
					iSettings.RemberPassword() );
			return item;
			}
		case ESysSettingListViewAutoLogin:
			{			
			CAknBinaryPopupSettingItem* item = new ( ELeave ) 
				CAknBinaryPopupSettingItem( 
					aId,
					iSettings.AutoLogin() );
			return item;
			}
	// ]]] end generated region [Initializers]
       case ESysSettingListViewIAPSetting:
            {           
             iApnItem = new ( ELeave ) 
                    CIApnDynamicArray( 
                        aId,
                        iSettings.IAPSetting() );
                return iApnItem;
            }
		}
		
	return NULL;
	}
	
/**
 * Edit the setting item identified by the given id and store
 * the changes into the store.
 * @param aIndex the index of the setting item in SettingItemArray()
 * @param aCalledFromMenu true: a menu item invoked editing, thus
 *	always show the edit page and interactively edit the item;
 *	false: change the item in place if possible, else show the edit page
 */
void CSysSettingList::EditItemL ( TInt aIndex, TBool aCalledFromMenu )
	{
	CAknSettingItem* item = ( *SettingItemArray() )[aIndex];
	switch ( item->Identifier() )
		{
	// [[[ begin generated region: do not modify [Editing Started Invoker]
	// ]]] end generated region [Editing Started Invoker]
	
		}
	
	CAknSettingItemList::EditItemL( aIndex, aCalledFromMenu );
	
	TBool storeValue = ETrue;
	switch ( item->Identifier() )
		{
	// [[[ begin generated region: do not modify [Editing Stopped Invoker]
	// ]]] end generated region [Editing Stopped Invoker]
	
		}
		
	if ( storeValue )
		{
		item->StoreL();
		//SaveSettingValuesL();
		}	
	}
/**
 *	Handle the "Change" option on the Options menu.  This is an
 *	alternative to the Selection key that forces the settings page
 *	to come up rather than changing the value in place (if possible).
 */
void CSysSettingList::ChangeSelectedItemL()
	{
	if ( ListBox()->CurrentItemIndex() >= 0 )
		{
		EditItemL( ListBox()->CurrentItemIndex(), ETrue );
		}
	}

/**
 *	Load the initial contents of the setting items.  By default,
 *	the setting items are populated with the default values from
 * 	the design.  You can override those values here.
 *	<p>
 *	Note: this call alone does not update the UI.  
 *	LoadSettingsL() must be called afterwards.
 */
void CSysSettingList::LoadSettingValuesL(TSysSetting aSetting)
	{
	// load values into iSettings
    if(aSetting.iAutoLogin.Compare(_L("1")) == 0)
        iSettings.SetAutoLogin(1);
    else
        iSettings.SetAutoLogin(0);
    if(aSetting.iRemberPassword.Compare(_L("1")) == 0)
        iSettings.SetRemberPassword(1);
    else
        iSettings.SetRemberPassword(0);
    
    iSettings.SetServerAddress(aSetting.iServerAddress);

    TInt idx =  -1;
    if(aSetting.iIapid.Length() > 0)
        {
        TLex iLex(aSetting.iIapid);
        iLex.Val(idx);
        }
    if(iApnItem->iIAP.Count() >= idx)
        iSettings.SetIAPSetting(idx);
    else
        iSettings.SetIAPSetting(0);
    
    idx = 80;
    if(aSetting.iPort.Length() > 0)
        {
        TLex iLex(aSetting.iPort);
        iLex.Val(idx);
        }
    iSettings.SetPort(idx);
	}
	
/**
 *	Save the contents of the setting items.  Note, this is called
 *	whenever an item is changed and stored to the model, so it
 *	may be called multiple times or not at all.
 */
void CSysSettingList::SaveSettingValuesL(TSysSetting& aSetting)
	{
	// store values from iSettings
    if(iSettings.AutoLogin())
        aSetting.iAutoLogin = _L("1");
    else
        aSetting.iAutoLogin = _L("0");
    if(iSettings.RemberPassword())
        aSetting.iRemberPassword = _L("1");
    else
        aSetting.iRemberPassword = _L("0");
    aSetting.iServerAddress = iSettings.ServerAddress();
    TBuf<4> aIndex;
    TInt idx = iSettings.IAPSetting();
    aIndex.Num(idx);
    aSetting.iIapid = aIndex;
    aIndex.Zero();
    idx = iSettings.Port();
    aIndex.Num(idx);
    aSetting.iPort = aIndex;
	}


/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CSysSettingList::HandleResourceChange( TInt aType )
	{
	CAknSettingItemList::HandleResourceChange( aType );
	SetRect( iAvkonViewAppUi->View( TUid::Uid( ESysSettingListViewId ) )->ClientRect() );
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	}
				
/** 
 * Handle key event (override)
 * @param aKeyEvent key event
 * @param aType event code
 * @return EKeyWasConsumed if the event was handled, else EKeyWasNotConsumed
 */
TKeyResponse CSysSettingList::OfferKeyEventL( 
		const TKeyEvent& aKeyEvent, 
		TEventCode aType )
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	// ]]] end generated region [Generated Contents]
	
	if ( aKeyEvent.iCode == EKeyLeftArrow 
		|| aKeyEvent.iCode == EKeyRightArrow )
		{
		// allow the tab control to get the arrow keys
		return EKeyWasNotConsumed;
		}
	
	return CAknSettingItemList::OfferKeyEventL( aKeyEvent, aType );
	}
				
